using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace myGame
{
    public class Pokemon : Microsoft.Xna.Framework.Game
    {
        int nb_food = 50;
        Viseur view;
        Principale pikachu;
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        List<Pokemom> pokemons;
        Random rnd;
        SpriteFont Font1;

        public Pokemon()
        {
            loadGraphic();
            Content.RootDirectory = "Content";
        }

        public void loadGraphic()
        {
            graphics = new GraphicsDeviceManager(this);
            this.graphics.PreferredBackBufferWidth = 1300;
            this.graphics.PreferredBackBufferHeight = 700;
            this.graphics.ApplyChanges();
        }

        protected override void Initialize()
        {
            rnd = new Random();
            this.pokemons = new List<Pokemom>(nb_food);
            for (int i = 0; i < nb_food; i++)
            {
                int type = rnd.Next(1, 4);
                Pokemom pk = new Pokemom(this, type);
                int posX = rnd.Next(0, GraphicsDevice.Viewport.Width - pk.getTexture().Width);
                int posY = rnd.Next(0, GraphicsDevice.Viewport.Height - pk.getTexture().Height);
                pk.setX(posX);
                pk.setY(posY);
                pokemons.Add(pk);
                Components.Add(pokemons.ElementAt(i));
            }

            this.IsMouseVisible = false;
            view = new Viseur(this);
            pikachu = new Principale(this);
            pikachu.setV(2);
            Components.Add(pikachu);
            Components.Add(view);
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Font1 = Content.Load<SpriteFont>("f1");
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            bool is_eat;
            pikachu.attaque(pokemons);
            for (int i = 0; i < pokemons.Count; i++)
            {
                is_eat = pikachu.eat(pokemons[i]);
                if (is_eat)
                {
                    Components.Remove(pokemons[i]);
                    pokemons.Remove(pokemons[i]);
                }
            }
            if (pokemons.Count == 0)
            {
                spriteBatch.Begin();
                spriteBatch.DrawString(Font1, "Bravo", new Vector2(GraphicsDevice.Viewport.Width/2, 10), Color.White);
                spriteBatch.End();
            }
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }
    }
}
